home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
dorskel2.arc
/
DOORSKL3.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-05
|
11KB
|
422 lines
/**********************************************/
/* */
/* XBBS Door Skeleton -- TC 2.0/MSC 6.0a code */
/* Copyright (c) 1990/91 by M. Kimes */
/* All Rights Reserved */
/* May be freely used for >>>FREE<<< programs */
/* as long as you don't try to save any souls */
/* (nasty habit) */
/* */
/**********************************************/
/* you can omit this module if you don't want the file reader readtext()...
has the same effect as including it but #defining NOREADFILE */
/**************************************************************************
Miscellaneous notes:
#define NOREADFILE and the file reader routine (readtext()) will NOT
be compiled (same as not including this module)
***************************************************************************/
#include "doorskel.h" /* XBBS Door include file */
#ifndef NOREADFILE
char _fastcall readtext (char *s,char paged) { /* Displays a text file
This thing's got all
sorts of fancy stuff
in it if you set paged = 1
Otherwise it does a pretty
dumb dump (ok for ANSI
files). s is the filename
to read. You can't do much
better with an online file
reader than this; it lets
you page forward and backward
through the text, search for
text strings, jump to home or
end of file, etc. All the
modern conveniences. */
long place2;
long place;
struct stat filestat;
char dostring[181];
char eeof=0;
word page;
char a;
long *pos;
word scratch;
FILE *fp;
register int x;
char prev=0;
char *here;
char *p;
char *tempo;
char temp='N';
char lastpage=0;
char searchstring[41];
char holdstring[41];
char teststring[81];
char findend=0;
char printanyway=0;
char linelen;
word lines=1;
*searchstring=0;
*holdstring=0;
if (stat(s,&filestat)) return 1;
if (!filestat.st_size) return 1;
if (!(fp=fopen(s,"rt"))) return 1;
cls();
printg("\x1b[0;1;33m");
page=0;
place2=ftell(fp);
if (!paged) {
printm("\r\n");
while (!feof(fp) && !ferror(fp)) {
if (!fgets(dostring,181,fp)) break;
if(ftell(fp)<=place2) {
while(fgetc(fp)=='\n'); /* Guard against Unix files & TC bug */
continue;
}
place2=ftell(fp);
printm(dostring);
if(numlines) {
lines++;
if(lines > numlines+1) {
lines=1;
printm("More? (Y/n) ");
fossil(FLUSHOUT,0);
fossil(PURGEIN,0);
linelen=*genin(1,0,1,1,YESNOM);
for (x=0;x<12;x++) printm(BACKSPACE);
if (linelen == 'N') break;
}
}
a = (char)toupper(inkey());
if (a=='S' || a==' ') break;
if (a=='P') my_sleep(5);
}
fclose(fp);
if (numlines) if (lines > numlines-11) hitreturn();
return 0;
}
/* Can be used to invoke an external reader like LIST
if (!baud && *reader) {
fclose(fp);
sprintf(dostring,"%s %s",reader,s);
system(dostring);
fputs("\x1b[2J\x1b[0m",stdout);
print_stat();
return 0;
}
*/
p=strchr(s,':'); /* Strip path off filename for display */
here=strrchr(s,'\\');
if(!p && !here) here=s;
else if (!here) here=&p[1];
else if (p) here= (p<here) ? &here[1] : &p[1];
else here++;
pos=(long *)malloc((word)(sizeof(long) * 1024));
if(!pos) {
fclose(fp);
printm("\r\nOut of memory\r\n");
exit(4);
}
printfm(" -=-=Reading: %s=-=- Page #%u\r\n",here,page+1);
printg("\x1b[0;1;32m");
if(skip_blanks(fp)) goto Abort;
place2=pos[0]=ftell(fp);
lines=1;
while (!feof(fp) && !ferror(fp)) {
if ((*searchstring && !printanyway) || findend) goto Searcher;
Domore:
if (!fgets(dostring,181,fp)) {
eeof++;
goto EEEOF;
}
if(ftell(fp) <= place2) {
while(fgetc(fp) == '\n'); /* Guard against Unix files & TC bug */
continue;
}
place2 = ftell(fp);
stripcr(dostring);
while (p=strchr(dostring,'\xc')) memmove(p,&p[1],strlen(&p[1])+1);
while (p=strchr(dostring,'\x1b')) memmove(p,&p[1],strlen(&p[1])+1);
if (!*dostring) {
if (prev) {
prev = 0;
eeof = skip_blanks(fp);
if (eeof) goto EEEOF;
else goto Domore;
}
else prev++;
}
else prev=0;
if (*searchstring) {
strcpy(teststring,dostring);
strupr(teststring);
if (strstr(teststring,searchstring)) {
printg("\x1b[0;1;37m");
printm(dostring);
printg("\x1b[0;1;32m");
lines++;
}
else {
printm(dostring);
lines++;
}
}
else {
printm(dostring);
lines++;
}
scratch=strlen(dostring);
p=dostring;
while(tempo=strchr(p,'\t')) {
p=tempo+1;
scratch+=7;
}
if (scratch < 79) {
printm("\r\n");
scratch++;
}
if (scratch>width) {
lines++;
}
a = (char)toupper(inkey());
if (a=='S' || a==' ') break;
if (a=='P' || a=='-') if (page) goto SkipThis;
if (a=='M') goto Direct;
Searcher:
if (lastpage) goto EEEOF;
if ((a=='N') || (*searchstring) || (findend)) {
if (findend) {
if (++page>1024) page=1;
pos[page]=ftell(fp);
}
place2=place=ftell(fp);
Domore1:
while (!feof(fp) && !ferror(fp)) {
Domore2:
if (!fgets(dostring,181,fp)) {
eeof++;
break;
}
if(ftell(fp)<=place2) {
while(fgetc(fp)=='\n'); /* Guard against Unix files & TC bug */
continue;
}
place2=ftell(fp);
while (p=strchr(dostring,'\xc')) memmove(p,&p[1],strlen(&p[1])+1);
while (p=strchr(dostring,'\x1b')) memmove(p,&p[1],strlen(&p[1])+1);
if (!strcmp(dostring,"\n")) {
if (prev) {
prev=0;
eeof=skip_blanks(fp);
if (eeof) break;
else goto Domore2;
}
else prev++;
}
else prev=0;
if (*searchstring) {
if (strstr(strupr(dostring),searchstring)) {
printanyway++;
break;
}
}
lines++;
scratch=strlen(dostring);
p=dostring;
while(tempo=strchr(p,'\t')) {
p=tempo+1;
scratch+=3;
}
if (scratch<79) {
scratch++;
}
if (scratch>width) lines++;
if (numlines) if ((lines >= numlines-2) || (feof(fp)) || (eeof)) break;
}
fossil(PURGEIN,0);
eeof=skip_blanks(fp);
if (feof(fp) || eeof || printanyway) {
if (*searchstring && !printanyway) {
printm("\r\nNot found...\r\n");
goto EEEOF;
}
fseek(fp,place,SEEK_SET);
clearerr(fp);
cls();
printg("\x1b[0;1;33m");
printfm(" -=-=Reading: %s=-=- Page #%u\r\n",here,page+1);
printg("\x1b[0;1;32m");
findend=prev=eeof=0;
lastpage = 1;
lines = 1;
goto Domore;
}
if ((*searchstring || findend) && !eeof) {
if (++page>1024) page=1;
pos[page]=place=place2=ftell(fp);
lines=1;
goto Domore1;
}
}
EEEOF:
if (numlines) if ((lines >= numlines-2) || (feof(fp)) || (eeof)) {
Direct:
linelen=0;
prev=0;
eeof=skip_blanks(fp);
if (feof(fp) || eeof) {
printg("\x1b[0;1;31m");
printm(" (END)");
linelen+=6;
printg("\x1b[0;1;33m");
temp='S';
}
else {
if (a=='M' || a=='N') goto SkipThis;
temp='N';
printg("\x1b[0;1;33m");
printm(" [F]ind [E]nd [N]ext [M]ore");
linelen+=27;
}
if (page) {
printm(" [H]ome [P]rior");
linelen+=15;
}
linelen+=sprintf(dostring," [O]ver [S]top: [%c] ",temp);
printm(dostring);
fossil(FLUSHOUT,0);
pos[page+1]=ftell(fp);
Getitagain:
fossil(PURGEIN,0);
a=*genin(1,0,1,1,ALLL);
SkipThis:
findend=0;
lastpage=0;
printanyway=0;
*searchstring=0;
switch (a) {
case 'S': goto Abort;
case 'H': if (!page) {
printm(BACKSPACE);
printm("\x7");
goto Getitagain;
}
page=0;
fseek(fp,0,SEEK_SET);
break;
case 'E': if (feof(fp) || eeof) {
printm(BACKSPACE);
printm("\x7");
goto Getitagain;
}
findend++;
break;
case 'F': if (feof(fp) || eeof) {
printm(BACKSPACE);
printm("\x7");
goto Getitagain;
}
printg("\x1b[0;1;35m");
printfm("\r\n Search string: [%s]\r\n -> ",holdstring);
strcpy(searchstring,genin(40,0,1,0,ALLL));
if (!*searchstring) {
if (*holdstring) {
strcpy(searchstring,holdstring);
if (++page>1024) page=1;
pos[page]=ftell(fp);
}
}
else strcpy(holdstring,searchstring);
fseek(fp,pos[page],SEEK_SET);
eeof=skip_blanks(fp);
break;
case 0: if (feof(fp) || eeof) goto Abort;
case 'N': if (feof(fp) || eeof) {
printm(BACKSPACE);
printm("\x7");
goto Getitagain;
}
a=0;
if (++page>1024) page=1;
pos[page]=ftell(fp);
break;
case 'O': fseek(fp,pos[page],SEEK_SET);
eeof=skip_blanks(fp);
break;
case 'P':
case '-': if (page) {
fseek(fp,pos[--page],SEEK_SET);
eeof=skip_blanks(fp);
break;
}
printm(BACKSPACE);
printm("\x7");
goto Getitagain;
case 'M': if (feof(fp) || eeof) {
printm("\x7");
printm(BACKSPACE);
goto Getitagain;
}
for (x=0;x < linelen;x++) printm(BACKSPACE);
printg("\x1b[0;1;32m");
if (numlines) lines=numlines-3;
goto Domore;
default: printm(BACKSPACE);
goto Getitagain;
}
place2=ftell(fp);
eeof=0;
cls();
printg("\x1b[0;1;33m");
if (findend || *searchstring) strcpy(teststring,"Scanning");
else strcpy(teststring,"Reading");
printfm(" -=-=%s: %s=-=- Page #%u\r\n",teststring,here,page+1);
printg("\x1b[0;1;32m");
lines=1;
}
}
Abort:
if(pos) free(pos);
fclose(fp);
return 0;
}
char _fastcall skip_blanks (FILE *fp) { /* Skips blank lines, what else? */
ulong pos;
char temp[81];
char eeof=0;
do {
pos=ftell(fp);
if (!fgets(temp,81,fp)) eeof++;
stripcr(temp);
} while ((!eeof) && (!feof(fp)) && (!*temp));
if (!eeof && !feof(fp)) fseek(fp,pos,SEEK_SET);
return eeof;
}
#endif